 //
//  MSDetailFootprintCell.m
//  SocietySale
//
//  Created by Mike on 16/11/10.
//  Copyright © 2016年 Mike. All rights reserved.
//

#define reserveImageHeight (SCREEN_WIDTH-24)*4/7

#import "MSDetailFootprintCell.h"

@interface MSDetailFootprintCell ()
//主图
@property (nonatomic, strong) DOImageView *mainImage;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *introductionLabel;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) UIView *bgView;
@end

@implementation MSDetailFootprintCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 8)];
    _headView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_headView];

    self.nameLabel = [self creatLabel:CGRectMake(12, _headView.bottom+10, SCREEN_WIDTH-100, 20)];
    self.timeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH-112, self.nameLabel.top, 100, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    
    self.mainImage = [[DOImageView alloc] initWithFrame:CGRectMake(12, self.nameLabel.bottom+10, SCREEN_WIDTH-24, reserveImageHeight)];
    self.mainImage.contentMode = UIViewContentModeScaleAspectFill;
    self.mainImage.layer.masksToBounds = YES;
    self.mainImage.userInteractionEnabled = YES;
    [self.contentView addSubview:self.mainImage];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [_mainImage addGestureRecognizer:tapGesture];
    
    _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(_mainImage.width-50, _mainImage.height-25, 40, 20)];
    _numLabel.font = HeitiFont(13);
    _numLabel.textAlignment = NSTextAlignmentRight;
    _numLabel.textColor = [UIColor whiteColor];
    [_mainImage addSubview:_numLabel];
    
    self.introductionLabel = [self creatLabel:CGRectMake(12, _mainImage.bottom, SCREEN_WIDTH-24, 40)];
    self.introductionLabel.numberOfLines = 0;
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, _introductionLabel.bottom, SCREEN_WIDTH, 40)];
//    _bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_bgView];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH-24, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_bgView addSubview:horizonLineView];
    
    UIView *horizonLineView1 =[[UIView alloc] initWithFrame:CGRectMake(0, 39.4, SCREEN_WIDTH, 0.6)];
    horizonLineView1.backgroundColor = kNormalGray;
    [_bgView addSubview:horizonLineView1];

    self.deleteBtn = [self creatBtnWithFrame:CGRectMake(SCREEN_WIDTH-140, 0, 40, 40) andImage:@"" andTitle:@"删除"];
    [self.deleteBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    
    self.goodBtn = [self creatBtnWithFrame:CGRectMake(SCREEN_WIDTH-90, 0, 40, 40) andImage:@"zan" andTitle:@""];
    self.badBtn = [self creatBtnWithFrame:CGRectMake(SCREEN_WIDTH-45, 0, 40, 40) andImage:@"cha" andTitle:@""];
    self.deleteBtn.tag = 10;
    self.goodBtn.tag = 11;
    self.badBtn.tag = 12;
    
    [self.goodBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 3, 0)];
    [self.badBtn setImageEdgeInsets:UIEdgeInsetsMake(3, 0, -3, 0)];
//    [_deleteBtn setBackgroundColor:kOrangeColor];
//    [_goodBtn setBackgroundColor:kCyanColor];
//    [_badBtn setBackgroundColor:kYellowColor];
}

- (void)setModel:(MSFootprintModel *)model{
    _model = model;
    CGSize size = [HDUtil sizeWithString:model.introduction font:HeitiFont(13) size:CGSizeMake(SCREEN_WIDTH-24, CGFLOAT_MAX)];
    if (size.height>30 && size.height<40) {//  两行  33
        self.introductionLabel.height = 60;
    }
    else{
        self.introductionLabel.height = 40;
    }
    if (model.imageArray.count>0) {
        self.mainImage.hidden = NO;
        propertyPicture* item = model.imageArray[0];
//        self.mainImage.imgURLStr = nil;
        self.mainImage.imgURLStr = item.path;//@"example.jpg";//
        self.numLabel.text = [NSString stringWithFormat:@"%ld张",model.imageArray.count];

        self.introductionLabel.top = self.mainImage.bottom;
        _bgView.top = self.introductionLabel.bottom;
    }
    else{
        self.mainImage.hidden = YES;
        self.introductionLabel.top = self.nameLabel.bottom;
        _bgView.top = self.introductionLabel.bottom;
    }
    self.nameLabel.text = model.name;
    self.introductionLabel.text = model.introduction;
    
    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    if (model.time.length) {
        NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
        textAttachment2.image = [UIImage imageNamed:@"time"];
        NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
        [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    }
    
    self.timeLabel.attributedText = timeAttstr;

    [_goodBtn setTitle:model.goodNum forState:UIControlStateNormal];
    [_badBtn setTitle:model.badNum forState:UIControlStateNormal];
    
    if (model.isGoodEd) {
        _goodBtn.enabled = NO;
        [_goodBtn setImage:[UIImage imageNamed:@"yizan"] forState:UIControlStateNormal];
    }
    else{
        _goodBtn.enabled = YES;
        [_goodBtn setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    }
    
    if (model.isBadEd) {
        _badBtn.enabled = NO;
        [_badBtn setImage:[UIImage imageNamed:@"chaping"] forState:UIControlStateNormal];
    }
    else{
        _badBtn.enabled = YES;
        [_badBtn setImage:[UIImage imageNamed:@"cha"] forState:UIControlStateNormal];
    }
    if (model.isSelf) {
        _deleteBtn.hidden = NO;
    }
    else{
        _deleteBtn.hidden = YES;
    }
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(13);
    label.textColor = kDarkGrayColor;
    [self.contentView addSubview:label];
    return label;
}

- (UIButton*)creatBtnWithFrame:(CGRect)frame andImage:(NSString*)imageStr  andTitle:(NSString*)title{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
//    [btn setBackgroundColor:kOrangeColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13];
    [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(footprintClick:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:btn];
    return btn;
}

- (void)tapClick{
    if ([self.delegate respondsToSelector:@selector(footprintClick:clickCellIndex:)]) {
        [self.delegate footprintClick:FootprintClickType_Image clickCellIndex:self.cellIndex];
    }
}

- (void)footprintClick:(UIButton*)btn{
    if ([self.delegate respondsToSelector:@selector(footprintClick:clickCellIndex:)]) {
        [self.delegate footprintClick:btn.tag-10 clickCellIndex:self.cellIndex];
    }
}

+ (CGFloat)cellHeightWithModel:(MSFootprintModel*)model{
    CGSize size = [HDUtil sizeWithString:model.introduction font:HeitiFont(13) size:CGSizeMake(SCREEN_WIDTH-24, CGFLOAT_MAX)];
    NSInteger height = 0;
    if (size.height>30 && size.height<40) {//  两行  33
        height = 20;
    }
    else{
        height = 0;
    }
    
    if (model.imageArray.count>0) {
        return reserveImageHeight + 128 + height;
    }
    else{
        return 118 + height;
    }
    return reserveImageHeight + 128;
}

@end
